<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/25
 * Time: 17:27
 */
namespace app\http\controller;

use app\admin\model\AdminUser;
use app\index\model\ShopList;
use think\Controller;
use think\facade\Session;

class IndexController extends Controller
{

    public function index(){
        $admin = (int)Session::get('shop_id');
        if (!$admin){
            return redirect(url("index/login"));
        }
        $user = ShopList::where('id',$admin)->field('id,shop_name')->find();
        $this->assign('admin',$user);
        return $this->fetch();
    }

    public function login(){
        if (request()->isPost()){
            $username = trim(input('post.username'));
            $password = trim(input('post.password'));
            $admin    = AdminUser::where('username',$username)->find();
            if (!$admin){
                return json(['code'=>0,'msg'=>'管理员不存在']);
            }
            $password = md5($username.md5($password));
            if ($password == $admin['password']){
                Session::set('shop_id',(int)$admin['shop_id']);
                return json(['code'=>1,'msg'=>'登录成功']);
            }else{
                return json(['code'=>0,'msg'=>'密码错误']);
            }
        }
        return $this->fetch();
    }



    public function loginOut(){
        Session::delete('shop_id');
        return json(['code'=>1,'msg'=>'退出成功']);
    }






}